package demo.mybatis.params.map;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class SmapleTest {

	String[] keys = new String[] { "name", "idNum", "cardType", "cardNum" };
	int[] types = new int[] { 1, 0, 1, 1 };

	@Test
	public void test() {
		System.out.println("123");
		Map<String, String> searchParam = new HashMap<>();
		searchParam.put("name1", "Poly");
		// 原始处理过程

		// 利用searchParam的键值对

		// 利用外部keys信息

		// 利用外部keys信息和状态信息
//		this.params(searchParam).forEach((k, v) -> System.out.println(k + "," + v));
//		this.params1(searchParam).forEach((k, v) -> System.out.println(k + "," + v));
//		this.params2(searchParam).forEach((k, v) -> System.out.println(k + "," + v));
		this.params3(searchParam).forEach((k, v) -> System.out.println(k + "," + v));
	}

	private Map<String, Object> params3(Map<String, String> searchParam) {
		Map<String, Object> params = new LinkedHashMap<>();
		for (int i = 0; i < keys.length; i++) {
			String k = keys[i];
			int t = types[i];
			String v = searchParam.getOrDefault(k, k);
			if (v != null && !v.equals("")) {
				if (t == 1) {

					params.put(k, "%" + v + "%");
				} else {
					params.put(k, v);
				}
			}
		}
		return params;
	}

	private Map<String, Object> params2(Map<String, String> searchParam) {
		Map<String, Object> params = new HashMap<>();
		for (String k : keys) {
			String v = searchParam.getOrDefault(k, null);
			if (v != null && !v.equals("")) {
				params.put(k, v);
			}
		}
		return params;
	}

	private Map<String, Object> params1(Map<String, String> searchParam) {
		Map<String, Object> params = new HashMap<>();
		for (Map.Entry<String, String> entry : searchParam.entrySet()) {
			String k = entry.getKey(), v = entry.getValue();
			if (v != null && !v.equals("")) {
				params.put(k, v);
			}
		}
		return params;
	}

	private Map<String, Object> params(Map<String, String> searchParam) {
		Map<String, Object> params = new HashMap<>();
		if (null != searchParam.get("name") && !"".equals(searchParam.get("name"))) {
			params.put("name", "%" + searchParam.get("name") + "%");
		}
		if (null != searchParam.get("idNum") && !"".equals(searchParam.get("idNum"))) {
			params.put("idNum", searchParam.get("idNum"));
		}
		if (null != searchParam.get("cardType") && !"".equals(searchParam.get("cardType"))) {
			params.put("cardType", searchParam.get("cardType"));
		}
		if (null != searchParam.get("cardNum") && !"".equals(searchParam.get("cardNum"))) {
			params.put("cardNum", searchParam.get("cardNum"));
		}
		if (null != searchParam.get("codeName") && !"".equals(searchParam.get("codeName"))) {
			params.put("codeName", "%" + searchParam.get("codeName") + "%");
		}
		if (null != searchParam.get("compyNum") && !"".equals(searchParam.get("compyNum"))) {
			params.put("compyNum", searchParam.get("compyNum"));
		}
		if (null != searchParam.get("cycleNbr") && !"".equals(searchParam.get("cycleNbr"))) {
			params.put("cycleNbr", searchParam.get("cycleNbr"));
		}
		return params;
	}
}
